/**
 * Created by cchen on 2015/6/8.
 */
'use strict';

angular.module('backofficeApp.bannerAdd.controllers', []).
    controller('bannerAddCtrl', ['$scope', '$routeParams', 'CommunityService', '$http', '$location', function ($scope, $routeParams, communityService, $http, $location) {
        $scope.communityLabelList = communityService.getAllCommunityService();

        $scope.type = $routeParams.type;

        $scope.currentPage = 1;
        $scope.numPages = 999;

        //小区多选框
        $scope.communityIds = [];
        var communityStr = '';
        $scope.toggle = function (ck, objId) {
            if (communityStr.indexOf(objId) != -1) {
                ck = true;
            }
            !ck ? $scope.communityIds.push(objId) : $scope.communityIds.remove(objId);

            communityStr = '';
            $scope.communityIds.forEach(function (community) {
                communityStr += community
            });
            $scope.getAllContent();
        };

        $scope.allCheck = function (ck) {
            if (!ck) {
                $scope.communityIds = [];
                communityStr = '';
                $("[name = chkItem]:checkbox").prop("checked", true);
                $scope.communityLabelList.data.forEach(function (community) {
                    $scope.communityIds.push(community.objId);
                    communityStr += community.objId
                })
                $scope.getAllContent();
            } else {
                $scope.communityIds = [];
                $("[name = chkItem]:checkbox").prop("checked", false);
                communityStr = '';
                $scope.getAllContent();
            }
        };

        $scope.selectFile = function (files) {
            $scope.image = files[0]
        };

        $scope.getAllContent = function () {

            var input = {
                page: $scope.currentPage,
                community: $scope.communityIds
            };
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/getAllContentListByCommunityId', input, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.questionList = data.data.question;
                $scope.activityList = data.data.activity;
                $scope.topicList = data.data.topic;
                $scope.noticeList = data.data.notice;
                $scope.lotteryList = data.data.lottery;
                $scope.iphoneList = data.data.iphone;
            }).error(function () {
            });
        };

        $scope.initCurrentPage = function () {
            $scope.currentPage = 1;
            $scope.getAllContent();
        };

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.getAllContent();
        };

        $scope.addBanner = function (id, openType, createdBy, url, title) {
            if ($scope.type == 1 && $scope.image == null) {
                alert('请选择图片');
                return;
            }
            if ($scope.type == 2 && ($scope.message == null || $scope.message == '')) {
                alert('请输入话题公告内容');
                return;
            }

            bootbox.confirm('是否确定添加?', function (result) {
                if (result) {
                    var fd = new FormData();
                    if ($scope.image != null) {
                        fd.append('image1', $scope.image);
                    }
                    if ($scope.message != null && $scope.message != '') {
                        fd.append('message', $scope.message);
                    }
                    fd.append('openType', openType);
                    fd.append('contentId', id);
                    fd.append('type', Number($scope.type));
                    if (createdBy != null && createdBy != undefined) {
                        fd.append('contentCreatedBy', createdBy);
                    }
                    if (url != null && url != undefined) {
                        fd.append('contentUrl', url);
                    }
                    $scope.communityIds.forEach(function (id) {
                        fd.append('community', id)
                    });
                    if (title != null) {
                        fd.append('title', title);
                    }

                    $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/addCommunityBanner', fd, {
                        transformRequest: angular.identity,
                        headers: {'Content-Type': undefined}
                    }).success(function (data) {
                        $location.path('/bannerList/' + $scope.type)
                    }).error(function () {
                    });
                }
            }).find("div.modal-dialog").css({width: '30%'});
        }

    }]).directive('paging', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {

                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.numPages ? scope.numPages : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('numPages', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.numPages;
                };

            }
        };
    });